package com.ht.medicaltreatment.view

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.DialogReplaceAdapter
import com.ht.medicaltreatment.bean.Drawblood
import kotlinx.android.synthetic.main.dialog_replace_caixue_layout.*
import kotlinx.android.synthetic.main.dialog_replace_layout.*
import kotlinx.android.synthetic.main.dialog_replace_layout.tvSubmit
import kotlinx.android.synthetic.main.dialog_replace_layout.tvUID

class ReplaceCaiXueDialog(context: Context) : BaseDialog(context,300f), OnItemClickListener {

    private lateinit var mClick:(Int,String,Int) -> Unit
    private var position = 0

    override fun getLayoutRes() = R.layout.dialog_replace_caixue_layout

    fun show(list:MutableList<Drawblood>,onClick:(Int,String,Int)->Unit){
        recyclerView.layoutManager = LinearLayoutManager(context)
        val adapter = DialogReplaceAdapter()
        recyclerView.adapter = adapter
        if(list.size > 0){
            list[0].isChecked = true
        }
        adapter.setNewInstance(list)
        adapter.setOnItemClickListener(this)
        mClick = onClick
        tvSubmit.setOnClickListener {
            if(tvUID.text == null){
                ToastUtils.showShort("没有UID数据")
                return@setOnClickListener
            }
            mClick(list[position].id,tvUID.text.toString(),position)
            dismiss()
        }
        super.show()
    }

    fun setUID(uid:String){
        tvUID.text = uid
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        adapter.data.forEachIndexed{index,item ->
            if(index == position){
                this@ReplaceCaiXueDialog.position = position
                (item as Drawblood).isChecked = true
            }else{
                (item as Drawblood).isChecked = false
            }
        }
        adapter.notifyDataSetChanged()
    }
}